package com.fenrirtec.aepp.common.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.condition.NewsNoticeCondition;
import com.fenrirtec.aepp.common.dto.NewsNoticeDto;
import com.fenrirtec.aepp.common.service.NewsNoticeService;
import com.fenrirtec.aepp.common.service.impl.NewsNoticeServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.JsonUtils;

public class NewsNoticeSearchAPI extends JSONActionSupport {

    private static final long serialVersionUID = 1L;

    private static final Logger logger = LoggerFactory.getLogger(NewsNoticeSearchAPI.class);

    private String param;

    private int _page;

    private int _rows;

    private int _start;

    private int _length;

    public String getParam() {
        return this.param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public int getPage() {
        return _page;
    }

    public void setPage(int page) {
        this._page = page;
    }

    public int getRows() {
        return _rows;
    }

    public void setRows(int rows) {
        this._rows = rows;
    }

    public int getStart() {
        return _start;
    }

    public void setStart(int start) {
        this._start = start;
    }

    public int getLength() {
        return _length;
    }

    public void setLength(int length) {
        this._length = length;
    }

    @Override
    protected List<ErrorInfo> doValidate() {
        return null;
    }

    @Override
    protected JSONObject doExecute() {

        NewsNoticeService service = new NewsNoticeServiceImpl();
        List<NewsNoticeDto> newsNoticeDtoList = null;
        Integer newsNoticeCount = 0;
        
        try {
            JSONObject jsonObj = JSONObject.parseObject(this.param);
            NewsNoticeCondition condition = new NewsNoticeCondition();

            if (jsonObj != null && !jsonObj.isEmpty()) {
                condition = JsonUtils.toBean(jsonObj, NewsNoticeCondition.class);
            }

            if (_page > 0 && _rows > 0) {
                condition.setPage(_page);
                condition.setRows(_rows);
            } else if (_start >= 0 && _length > 0) {
                condition.setPage(_start / _length + 1);
                condition.setRows(_length);
            }

            newsNoticeCount = service.countByCnd(condition);
            newsNoticeDtoList = service.searchByCnd(condition);

        } catch (DatabaseException e) {
            if (logger.isErrorEnabled()) {
                logger.error("database error occurred.", e);
            }
            throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
        } catch (UnexpectedException e) {
            if (logger.isErrorEnabled()) {
                logger.error("unexpected error occurred.", e);
            }
            throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
        }

        JSONObject root = new JSONObject();
        if (_start >= 0 && _length > 0) {
            root.put("recordsTotal", newsNoticeCount);
            root.put("recordsFiltered", newsNoticeCount);
        } else {
            root.put("total", newsNoticeCount);
        }
        if (newsNoticeDtoList != null && newsNoticeDtoList.size() > 0) {
            root.put("news_notice", JsonUtils.fromBean(newsNoticeDtoList));
        } else {
            root.put("news_notice", new JSONArray());
        }
        return root;
    }

}
